﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using GameEngine.SceneGraph;


namespace Redland2
{
    public class Gun : Entity
    {
        #region members
        private Vector2 _direction;
        private TimeSpan _lastShot;
        private int _damage;
        private double _fireDelay;
        private float _accuracy; // not implemented yet
        #endregion members

        #region properties

        public Vector2 Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        public TimeSpan LastShot
        {
            get { return _lastShot; }
            set { _lastShot = value; }
        }

        public double FireDelay
        {
            get { return _fireDelay; }
            set { _fireDelay = value; }
        }

        public int Damage
        {
            get { return _damage; }
            set { _damage = value; }
        }

        public float Accuracy
        {
            get { return _accuracy; }
            set { _accuracy = value; }
        }

        #endregion properties

        #region constructors
        public Gun(GameScene inputScene, int inputIndex, Player inputOwner, Vector2 position ): base(inputScene, inputIndex, GameScene.Current.Content.Load<Texture2D>("gun"), position)
        {
            SetupGun();
            this.Owner = inputOwner;
            //scene._entities.Add(this);
        }

        private void SetupGun()
        {
            this.Layer = 0.95F;
            this.type = "gun";
            this._accuracy = 1.0F;
            this._fireDelay = 0;
            this._damage = 0;
            this._lastShot = scene._time;
            //this.SetOffset(GameEngine.Drawables.Anchor.Center);
        }
        #endregion constructors

        #region methods

        public void RotateTodirection()
        {
            _direction = Owner.TargetPos - (Owner.Position + _direction * 5);
            _direction.Normalize();
            this.Angle = (float)Math.Atan2(_direction.Y, _direction.X);
        }

        public override void Update(GameTime gameTime)
        {
            RotateTodirection();
            this.Position = Owner.Position + _direction*5; // offset gun to right side of player
            base.Update(gameTime);
            _collisions.RemoveRange(0, _collisions.Count); // no collision checking for guns.
            if( Owner == null)
                this.Kill();
        }

        public virtual void Fire()
        {
            if (scene._time.Subtract(TimeSpan.FromMilliseconds(_fireDelay)) > _lastShot && this.IsAlive())
            {
                _lastShot = scene._time;
                Vector2 offsetPosition = this.Position;
                offsetPosition += Vector2.Transform(_direction, Matrix.CreateRotationZ(MathHelper.PiOver2)) * 4; // offset bullet to the right of center of player
                offsetPosition += _direction * this.TextureWidth; // offset bullet to tip of barrel
                Vector2 shotDirection = Owner.TargetPos - offsetPosition;
                shotDirection.Normalize();
                shotDirection *= 500;
                shotDirection += new Vector2(rand.Next() % 200 - 100, rand.Next() % 200 - 100) * (1 - _accuracy);

                scene._entities.Add(new Bullet(this.scene, this.scene._entities.Count, this.Owner, offsetPosition, shotDirection, _damage ));
            }
        }

        public override void Kill()
        {
            base.Kill();
        }

        #endregion methods 
    }
}
